package com.sense.kukoo.common.anim

import android.view.View

class UpOrDownAnimHelper {

    private val TAG = UpOrDownAnimHelper::class.simpleName

    private lateinit var defView: View

    private var defAnimateDuration = 400L
    private var mMaxTranslationY: Float = 0F
    private var mIsUp = false

    private var mEndActionOfShow: ()->Unit = {}
    private var mEndActionOfHide: ()->Unit = {}

    private val mShowRunnable = {
        mEndActionOfShow()
    }

    private val mHideRunnable = {
        mEndActionOfHide()
    }

    fun setView(v: View){
        defView = v
    }

    fun setMaxTranslationY(max: Float){
        mMaxTranslationY = max
    }

    fun setAnimateDuration(duration: Long){
        defAnimateDuration = duration
    }

    fun setShowAction(action: ()->Unit){
        mEndActionOfShow = action
    }

    fun setHideAction(action: ()->Unit){
        mEndActionOfHide = action
    }

    fun animateToShow(){
        if (!mIsUp) {
            mIsUp = true
            defView.clearAnimation()
            defView.animate().translationY(0F)
                .setDuration(defAnimateDuration)
                .withEndAction(mShowRunnable)
                .start()
        }
    }

    fun animateToHide(){
        if (mIsUp) {
            mIsUp = false
            defView.clearAnimation()
            defView.animate().translationY(mMaxTranslationY)
                .setDuration(defAnimateDuration)
                .withStartAction(mHideRunnable)
                .start()
        }
    }

}